package zhizhemodi.tech.short_term_bwork.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import zhizhemodi.tech.short_term_bwork.entity.Plan;
import zhizhemodi.tech.short_term_bwork.mapper.PlanMapper;

@Repository
public class PlanService {
    
    @Autowired
    PlanMapper planMapper;

    public List<Plan> getPlanList(
        Integer dId,
        Integer rowNum,
        Integer pageNum
    ) {
        QueryWrapper<Plan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_id", dId);
        queryWrapper.last(" limit " + pageNum * rowNum + "," + rowNum);
        return planMapper.selectList(queryWrapper);
    }

    public List<Plan> findPlan(String planName) {
        QueryWrapper<Plan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plan_name", planName);
        return planMapper.selectList(queryWrapper);
    }

    public void addPlan(Plan plan) {
        List<Plan> plans = planMapper.selectList(null);
        Integer id = 0;
        if(plans.size() != 0) id = plans.get(plans.size() - 1).getPlanId() + 1;
        if(findPlan(plan.getPlanName()).size() == 0) {
            plan.setPlanId(id);
            planMapper.insert(plan);
        }
    }
}
